import re
import urllib.error,urllib.request
from bs4 import BeautifulSoup
import xlwt
def main():
    baseurl="https://search.jd.com/Search?keyword=%E8%B7%AF%E7%94%B1%E5%99%A8&enc=utf-8&spm=a.0.0&pvid=7b092906b19146eab7bc825dd3b8e1c9?start="
    datalist=getdata(baseurl)
    print(askurl(baseurl))
findlist=re.compile(r'<a href="(.*)" onclick=".*" target=".*" title=".*">')
findimg=re.compile(r'<img.*src="(.*?)".*>')
findtitle=re.compile(r'<span class=".*"><a.*>(.*)</a></span>')
findcontent=re.compile(r'<em>(.*?)<font class=".*">(.*?)</font>(.*?)<font class=".*">(.*?)</font></em>',re.S)
findprice=re.compile(r'<em>￥</em><i>(\d*)</i>',re.S)
findinq=re.compile(r'<i class=".*" id=".*">(.*)</i>',re.S)

def getdata(baseurl):
    datalist=[]
    for i in range(0,1):
        url=baseurl+str(i*200)
        html=askurl(url)
        bs=BeautifulSoup(html,'html.parser')
        for item in bs.find_all('div',class_="gl-i-wrap"):
            try:
                item=str(item)

                data=[]

                list=re.findall(findlist,item)[0]
                data.append(list)
                img=re.findall(findimg,item)[0]
                data.append(img)

                title=re.findall(findtitle,item)[0]
                data.append(title)


                content=re.findall(findcontent,item)[0]
                data.append(content)
                print(data)

                price=re.findall(findprice,item)[0]
                data.append(price)

                inq=re.findall(findinq,item)[0]
                data.append(inq)

                datalist.append(data)


            except:
                pass
    return datalist
def askurl(url):
    head={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}
    req=urllib.request.Request(url,headers=head)
    try:
        response=urllib.request.urlopen(req)
        html=response.read().decode("utf-8")
    except urllib.error.URLError as i:
        if hasattr(i,"code"):
            print(i.code)
        if hasattr(i,"reason"):
            print(i.reason)
    return html













if __name__ == '__main__':
    main()
